package com.wuliaogame.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuliaogame.auto.JSONAuthentication;
import com.wuliaogame.entity.PClub;
import com.wuliaogame.entity.PPlayer;
import com.wuliaogame.mapper.PHeroMapper;
import com.wuliaogame.mapper.PPlayerMapper;
import com.wuliaogame.mapper.PUserMapper;
import com.wuliaogame.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuliaogame
 * @since 2022-04-21
 */
@RestController
@RequestMapping("/p-player")
public class PPlayerController extends JSONAuthentication {
    @Autowired
    PPlayerMapper pPlayerMapper;
    @Autowired
    PUserMapper pUserMapper;
    @Autowired
    PHeroMapper pHeroMapper;
    @ApiOperation(value = "添加用户成为职业选手")
    @PostMapping("/add")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestBody PPlayer player) throws Exception {
      if (player.getAge()>=18){
          QueryWrapper wrapper=new QueryWrapper();
          wrapper.eq("user_id",player.getUserId());
          if(pPlayerMapper.selectOne(wrapper)!=null){
              Result error = Result.error().message("该用户已是职业选手，不可添加");
              this.WriteJSON(request, response, error);
          }else{
              if(pPlayerMapper.insert(player)>0){
                  Result ok = Result.ok().message("添加成功");
                  this.WriteJSON(request, response, ok);
              }else{
                  Result error = Result.error().message("添加失败");
                  this.WriteJSON(request, response, error);
              }
          }
      }else{
          Result error = Result.error().message("未满18岁");
          this.WriteJSON(request, response, error);
      }

    }
    @ApiOperation(value = "移除用户的职业选手身份")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=pPlayerMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改职业选手的信息")
    @PostMapping("/upd")
    public void upd(HttpServletRequest request, HttpServletResponse response, @RequestBody PPlayer player) throws Exception {

            if(pPlayerMapper.updateById(player)>0){
                Result ok = Result.ok().message("修改成功");
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error().message("修改失败");
                this.WriteJSON(request, response, error);
            }
    }

    @ApiOperation(value = "修改职业选手的信息状态")
    @PostMapping("/updStatus")
    public void updStatus(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("status")String status) throws Exception {
        PPlayer pPlayer = pPlayerMapper.selectById(id);
        pPlayer.setStatus(status);
        if(pPlayerMapper.updateById(pPlayer)>0){
            Result ok = Result.ok().message("审核成功");
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("审核失败");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改职业选手的rank分")
    @PostMapping("/updRank")
    public void updRank(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("rank")int rank) throws Exception {
        PPlayer pPlayer = pPlayerMapper.selectById(id);
        pPlayer.setRank(rank);
        if(pPlayerMapper.updateById(pPlayer)>0){
            Result ok = Result.ok().message("修改成功");
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("修改失败");
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查找全部职业选手")
    @PostMapping("/find")
    public void findBynamea(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {

        Page<PPlayer> page = new Page<>(current, size);
        Page<PPlayer> pages = pPlayerMapper.selectPage(page, null);
        List<PPlayer> list = pages.getRecords();
        int total = pPlayerMapper.selectCount(null);
        for (PPlayer a:list){
        a.setPUser(pUserMapper.selectById(a.getUserId()));
        a.setPHero(pHeroMapper.selectById(a.getHeId()));
        }
        if (list.size() > 0) {
            Result ok = Result.ok().data("zyxs", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有职业选手");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "查找全部职业选手根据选手姓名")
    @PostMapping("/findByName")
    public void findByName(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("name")String name) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("name",name);
        Page<PPlayer> page = new Page<>(current, size);
        Page<PPlayer> pages = pPlayerMapper.selectPage(page, wrapper);
        List<PPlayer> list = pages.getRecords();
        int total = pPlayerMapper.selectCount(wrapper);
        for (PPlayer a:list){
            a.setPUser(pUserMapper.selectById(a.getUserId()));
            a.setPHero(pHeroMapper.selectById(a.getHeId()));
        }
        if (list.size() > 0) {
            Result ok = Result.ok().data("zyxs", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有职业选手");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "根据用户id查找职业选手")
    @PostMapping("/findByuserId")
    public void findByuserId(HttpServletRequest request, HttpServletResponse response,@RequestParam("userId")int userId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("user_id",userId);
        PPlayer pPlayer = pPlayerMapper.selectOne(wrapper);
        if (pPlayer!=null) {
            pPlayer.setPUser(pUserMapper.selectById(pPlayer.getUserId()));
            pPlayer.setPHero(pHeroMapper.selectById(pPlayer.getHeId()));
            Result ok = Result.ok().data("pPlayer", pPlayer);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有职业选手");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "查找全部职业选手根据rank分排序")
    @PostMapping("/findByRank")
    public void findByRank(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByDesc("rank");
        Page<PPlayer> page = new Page<>(current, size);
        Page<PPlayer> pages = pPlayerMapper.selectPage(page, wrapper);
        List<PPlayer> list = pages.getRecords();
        int total = pPlayerMapper.selectCount(wrapper);
        for (PPlayer a:list){
            a.setPUser(pUserMapper.selectById(a.getUserId()));
            a.setPHero(pHeroMapper.selectById(a.getHeId()));
        }
        if (list.size() > 0) {
            Result ok = Result.ok().data("zyxs", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有职业选手");
        this.WriteJSON(request, response, error);
    }
}

